package com.feng.zhihao.apigeteway.config.spring;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableTransactionManagement
//扫描dao层接口
@EnableJpaRepositories(basePackages = "com.feng.zhihao.apigeteway.dao")
//扫描实体类
@EntityScan(basePackages = "com.feng.zhihao.apigeteway.entity")
public class DAO {
	public static DataSource dataSource;
	@Bean(destroyMethod = "close")
	@Primary
	public DataSource dataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://vm:12111/api-geteway?characterEncoding=utf8&useSSL=true");
		dataSource.setUsername("root");
		dataSource.setPassword("77777qa");
		dataSource.setFilters("mergeStat");
		dataSource.getProxyFilters().add(slf4jLogFilter());
		DAO.dataSource=dataSource;
		return dataSource;
	}
	@Bean
	public LocalSessionFactoryBean getLocalSessionFactoryBean(@Autowired DataSource dataSource){
		LocalSessionFactoryBean sessionFactoryBean=new LocalSessionFactoryBean();
		sessionFactoryBean.setDataSource(dataSource);
		return sessionFactoryBean;
	}
	public Slf4jLogFilter slf4jLogFilter(){
		Slf4jLogFilter logFilter=new Slf4jLogFilter();
		logFilter.setConnectionLogEnabled(true);
		logFilter.setStatementLogEnabled(true);
		logFilter.setResultSetLogEnabled(true);
		logFilter.setStatementExecutableSqlLogEnable(true);
		return logFilter;
	}

}